AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( "shared.lua" )


function ENT:SetPlayer(ply)
	self:SetOwner(ply)
end

function ENT:Arm()
	if self.BombTime == nil then
		self.BombTime = CurTime() + 1
		self:EmitSound(self.ArmAlert,100,math.random(90,100))
	end
end

function ENT:Explode( )
	if !self.Exploded then
	
		self.Exploded = true
		
		self:EmitSound(ChooseRandom(GameSounds.BombExplosion2),math.random(100,130),math.random(75,135))
		
		local effectdata = EffectData()
		effectdata:SetOrigin( self:GetPos() )
		util.Effect( "mine_explosion", effectdata )
		
		self:ExplosionDamage(self:GetOwner(),self:GetPos(),225,350)// 250 dmg
		self:Remove()
	end
end

function ENT:Initialize()

	NUM_MINES = NUM_MINES + 1

	util.PrecacheModel( "models/props_interiors/pot01a.mdl" )
	self:SetModel( "models/props_interiors/pot01a.mdl" )

	self:PhysicsInit( SOLID_VPHYSICS )
	self:SetMoveType( MOVETYPE_VPHYSICS )
	self:SetSolid( SOLID_VPHYSICS )
	
	self.HitCounter = math.random(10,20)
	self.HitSound = {"physics/metal/metal_computer_impact_bullet1.wav",
	"physics/metal/metal_computer_impact_bullet2.wav",
	"physics/metal/metal_computer_impact_bullet3.wav"}
	
	for k,v in pairs(self.HitSound) do
		util.PrecacheSound(v)
	end

	local phys = self:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:SetMass( 60 )
		phys:EnableDrag(true)
		phys:EnableCollisions(true)
		phys:SetMaterial("gmod_bouncy")
	end
	
	self:SetAngles(Angle(math.random(-179,180),math.random(-179,180),math.random(-179,180)))
	
	self.BombTime = nil
	self.CleanupTime = CurTime() + 600 //10 minutes
	
	self.Exploded = false
	self.CheckTime = 1
	self.CheckBeep = Sound("buttons/button18.wav")
	self.ArmAlert = Sound("npc/roller/mine/rmine_predetonate.wav")
	
end 

function ENT:OnRemove()
	NUM_MINES = NUM_MINES - 1
end

function ENT:Think()

	if NUM_MINES > MAX_NUM_MINES then
		self:Arm()
	end

	if self:WaterLevel() > 1 then
		self:Arm()
	end

	if self.DelaySound then
		if self.DelaySound < CurTime() then
			self:EmitSound(self.PSound,100,self.DPitch)
			self.DelaySound = nil
			self:SetPhysicsAttacker( self:GetOwner( ) )
		end
	end
	if self.DelayParent then
		if self.DelayParent < CurTime() then
			self:SetParent(nil)
			self.DelayParent = nil
			local phys = self:GetPhysicsObject()
			if phys:IsValid() then
			phys:ApplyForceCenter(self:GetOwner():GetVehicle():GetVelocity())
			end
		end
	end

	self.CheckTime = self.CheckTime + 1
	if self.CheckTime > 20 then
		self.CheckTime = 1
		local enttbl = ents.FindInSphere( self:GetPos(), self.Radius )
		for k,v in pairs(enttbl) do
			if v:IsPlayer() then
				if v:Team() != self:GetOwner():Team() then
					self:Arm()
					return
				end
			elseif v:GetClass() == "vehicle_*" then
				if v:GetOwner():Team() != self:GetOwner():Team() then
					self:Arm()
					return
				end
			end
		end
		self:EmitSound(self.CheckBeep,90,math.random(90,110))
		local effectdata = EffectData()
		effectdata:SetEntity(self)
		effectdata:SetScale(self:GetOwner():Team())
		util.Effect("mine_flash", effectdata)
	end
	if self.BombTime then
		if self.BombTime < CurTime() then
			self:Explode()
		end
	end
	if self.CleanupTime < CurTime() then
		self:Arm()
	end
end

function ENT:OnTakeDamage( dmg )
	if dmg:GetAttacker():IsPlayer() then
		self:EmitSound(self.HitSound[math.random(1,3)],100,math.random(90,110))
		if dmg:GetAttacker():Team() != self:GetOwner():Team() then
			self.HitCounter = self.HitCounter - 1
			if self.HitCounter < 1 then
				self:Arm( )
			end
		end
	end
end

function ENT:PhysicsCollide( data, physobj )
	if (data.Speed > 120 && data.DeltaTime > 0.2 ) then
		local s = "physics/metal/metal_solid_impact_soft"..math.random(1,3)..".wav"
		util.PrecacheSound(s)
		self:EmitSound(s,math.random(60,100),math.random(80,120))
	end
end
